
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>C语言入门(2) - 小林的博客</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="4. 字符串、转义字符
 &amp;quot;abcdefg&amp;quot;  这种由双引号（Double Quote）引起来的一串字符称为字符串字面（String Literal），或者简称字符串。字符串的结,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="小林的博客" type="application/atom+xml"> 
    <link rel="icon" href="/my_blog.gitee.io/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="C语言入门(2) - 小林的博客"/>
    <meta name="twitter:description" content="4. 字符串、转义字符
 &amp;quot;abcdefg&amp;quot;  这种由双引号（Double Quote）引起来的一串字符称为字符串字面（String Literal），或者简称字符串。字符串的结,"/>
    
    
    
    
    <meta property="og:site_name" content="小林的博客"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="C语言入门(2) - 小林的博客"/>
    <meta property="og:description" content="4. 字符串、转义字符
 &amp;quot;abcdefg&amp;quot;  这种由双引号（Double Quote）引起来的一串字符称为字符串字面（String Literal），或者简称字符串。字符串的结,"/>
    
<link rel="stylesheet" href="/my_blog.gitee.io/css/diaspora.css">

    <script>window.searchDbPath = "/my_blog.gitee.io/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">小林的博客</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://lin2c.gitee.io/my_blog.gitee.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">C语言入门(2)</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">C语言入门(2)</h1>
        <div class="stuff">
            <span>二月 14, 2023</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/my_blog.gitee.io/tags/%E5%AD%97%E7%AC%A6%E4%B8%B2/" rel="tag">字符串</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/my_blog.gitee.io/tags/%E9%80%89%E6%8B%A9%E8%AF%AD%E5%8F%A5/" rel="tag">选择语句</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="4-字符串、转义字符"><a href="#4-字符串、转义字符" class="headerlink" title="4. 字符串、转义字符"></a>4. 字符串、转义字符</h1><blockquote>
<p> <code>&quot;abcdefg&quot;</code>  这种由双引号（Double Quote）引起来的一串字符称为字符串字面（String Literal），或者简<font color="#ed83ab" size=3 face="黑体" >称字符串</font>。<font color="#ed83ab" size=3 face="黑体" >字符串的结束标志是一个 \0 的转义字符。在计算字符串长度的时候 \0 是结束标志，不算作字符串内容。 </font></p>
</blockquote>
<h2 id="4-2-字符串定义"><a href="#4-2-字符串定义" class="headerlink" title="4.2 字符串定义"></a>4.2 字符串定义</h2><p><code>&quot;char a[] = &quot;abcde&quot;</code> 这种方法，末尾会自动追加\0<br> <code>&quot;char a[] = &#123;&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;\0&#39;&#125;</code> 这种方法，需要手动追加\0，否则字符串会没有结束标志。<br> <code>&quot;char a[20] = &#123;&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;&#125;</code> 这种方法，定义了长度为20，那么后面长度不足部分会自动补\0，所以也是可以的。</p>
<p>接下来我们试一下</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> a[] = <span class="string">&quot;abcde&quot;</span>;</span><br><span class="line">	<span class="type">char</span> b[] = &#123; <span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>,<span class="string">&#x27;\0&#x27;</span> &#125;;</span><br><span class="line">	<span class="type">char</span> c[<span class="number">20</span>] = &#123; <span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span> &#125;;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %s\n&quot;</span>, a);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;b = %s\n&quot;</span>, b);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;c = %s\n&quot;</span>, c);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/b3b5f3c24c5b4da6a94e06d374736416.png" alt="在这里插入图片描述"></p>
<p>这些都是可以的，但是如果在没有限制的长度的情况下，b是一定要加\0的，否则字符串没有结束标志，会非法访问后面的内容。<br><img src="https://img-blog.csdnimg.cn/88cebb17d7c14aacb2769cad51465cbf.png" alt="在这里插入图片描述"><br>字符串没有结束标志，就会访问随即内容。</p>
<h2 id="4-2-字符串长度"><a href="#4-2-字符串长度" class="headerlink" title="4.2 字符串长度"></a>4.2 字符串长度</h2><blockquote>
<p><font color="#ed83ab" size=4 face="黑体" >字符串长度是从第一个字符开始，到\0停止，不包括\0。</font></p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> a[] = <span class="string">&quot;abcde&quot;</span>; <span class="comment">//从a开始，到e后一个\0结束，长度5</span></span><br><span class="line"><span class="type">char</span> b[] = &#123; <span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>,<span class="string">&#x27;\0&#x27;</span>&#125;;<span class="comment">//从a开始，到\0结束，不包括\0，长度5</span></span><br><span class="line"><span class="type">char</span> c[<span class="number">20</span>] = &#123; <span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span> &#125;;<span class="comment">//一样，长度5</span></span><br></pre></td></tr></table></figure>

<p>有一个strlen函数，可以获取一个字符串的长度，我们只需要引入string.h库，即可使用。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> a[] = <span class="string">&quot;abcde&quot;</span>;</span><br><span class="line">	<span class="type">char</span> b[] = &#123; <span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>,<span class="string">&#x27;\0&#x27;</span>&#125;;</span><br><span class="line">	<span class="type">char</span> c[<span class="number">20</span>] = &#123; <span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span> &#125;;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;lena = %d\n&quot;</span>, <span class="built_in">strlen</span>(a));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;lenb = %d\n&quot;</span>, <span class="built_in">strlen</span>(b));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;lenc = %d\n&quot;</span>, <span class="built_in">strlen</span>(c));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/a282b43194624a419df341cd8d367ba3.png" alt="在这里插入图片描述"><br>我们发现长度都是5，因为后面都有\0作为结束标志，但如果我们把b的\0去掉，那么 b的长度就变成了随机值。<br><img src="https://img-blog.csdnimg.cn/d03c439bea0548478df687b8d3774c00.png" alt="在这里插入图片描述"><br>所以字符串的长度是从第一个字符开始，到\0结束，其中不包括\0。</p>
<h2 id="4-3-字符串大小"><a href="#4-3-字符串大小" class="headerlink" title="4.3 字符串大小"></a>4.3 字符串大小</h2><blockquote>
<p><font color="#ed83ab" size=4 face="黑体" >字符串的大小就是它开辟了多少空间，那么大小就是多少。如果定义时没有明确数组开辟多少空间，那么按数组元素个数计算大小。</font></p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> a[] = <span class="string">&quot;abcde&quot;</span>; <span class="comment">//包括\0 ， 6个字节</span></span><br><span class="line"><span class="type">char</span> b[] = &#123; <span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>&#125;; <span class="comment">//5个字节，元素个数</span></span><br><span class="line"><span class="type">char</span> c[<span class="number">20</span>] = &#123; <span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span> &#125;;<span class="comment">//有明确开辟20个空间大小，20字节</span></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/e3267ffce0d548f6b783f3cfb6befd6e.png" alt="在这里插入图片描述"><br>sizeof是一个关键字，会返回变量占用空间的大小。</p>
<h2 id="4-4-转义字符"><a href="#4-4-转义字符" class="headerlink" title="4.4 转义字符"></a>4.4 转义字符</h2><table>
<thead>
<tr>
<th>转义字符</th>
<th>释义</th>
</tr>
</thead>
<tbody><tr>
<td>\?(不常用)</td>
<td>在书写连续多个问号时使用，防止他们被解析成三字母词</td>
</tr>
<tr>
<td>\‘</td>
<td>用于表示字符常量’</td>
</tr>
<tr>
<td>\“</td>
<td>用于表示一个字符内部的双引号</td>
</tr>
<tr>
<td>\\</td>
<td>用于表示一个反斜杠，防止它被解释为一个转义序列符</td>
</tr>
<tr>
<td>\a</td>
<td>警告字符，蜂鸣</td>
</tr>
<tr>
<td>\b</td>
<td>退格字符</td>
</tr>
<tr>
<td>\f</td>
<td>进纸符</td>
</tr>
<tr>
<td>\n</td>
<td>换行符</td>
</tr>
<tr>
<td>\r</td>
<td>回车</td>
</tr>
<tr>
<td>\t</td>
<td>水平制表符</td>
</tr>
<tr>
<td>\v</td>
<td>垂直制表符</td>
</tr>
<tr>
<td>\ddd</td>
<td>ddd表示1~3个八进制的数字。 如： \130 X</td>
</tr>
<tr>
<td>\xdd</td>
<td>dd表示2个十六进制数字。 如： \x30 0</td>
</tr>
</tbody></table>
<p>接下来演示几个转义字符。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;ABCD\b\n&quot;</span>); <span class="comment">//退格+回车，输出ABC换行</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;ABCD\\\n &quot;</span>);<span class="comment">// \加换行，输出ABCD\换行</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,<span class="string">&#x27;\010&#x27;</span>); <span class="comment">//表示8进制的123，输出8回车</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="string">&#x27;\x10&#x27;</span>);<span class="comment">//16进制的ff，16回车</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/556c13c7db2a49339aab8e8a8bd21d18.png" alt="在这里插入图片描述"></p>
<p><font color="#ed83ab" size=4 face="黑体" >注意！所有的转义字符，它都只占一个字节。因为转义字符归根结底还是字符！字符占1个字节。</font></p>
<h1 id="5-选择语句"><a href="#5-选择语句" class="headerlink" title="5.选择语句"></a>5.选择语句</h1><blockquote>
<p><font color="#ed83ab" size=4 face="黑体" >人无时无刻都在做选择，程序也需要做选择。当我们期望程序达到某个条件时，就做出不同的选择时。我们就需要选择语句，来控制程序走哪条路。</font></p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/9635b7a21fd443a5b325e8d1fd510798.png" alt="在这里插入图片描述"></p>
<h2 id="5-2-if单分支"><a href="#5-2-if单分支" class="headerlink" title="5.2 if单分支"></a>5.2 if单分支</h2><blockquote>
<p><font color="#ed83ab" size=4 face="黑体" >简单来说，单分支就是你要不要选择这条路。最终的终点是一样的，但中间会不会有小插曲，取决于你的选择。</font></p>
</blockquote>
<p>假如你现在放学了，你可以直接回家。但你也可以选择先去网吧，再回家，而去网吧的这个过程就是你中间的选择。</p>
<p>但是你要去网吧，必须要有个条件，当满足了什么条件时，你才会选择去网吧。如果此时你身上有超过五块钱时，先去网吧，再回家。<br><img src="https://img-blog.csdnimg.cn/30eec2d5c68842c3b96ebcbd4a76aab9.png" alt="在这里插入图片描述"><br>所以我们可以用程序来模拟上面的过程。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> money = <span class="number">6</span>; <span class="comment">//你身上的钱</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span> (money &gt; <span class="number">5</span>) <span class="comment">//如果你身上的钱超过5块</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//去网吧</span></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;去网吧-&gt;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//不管如何，你都要回家</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;回家&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时我有6块钱，所以可以去网吧。<br><img src="https://img-blog.csdnimg.cn/3752645e220a44f7b35f713b4780f3bc.png" alt="在这里插入图片描述"><br>但如果我们把 money改成小于5呢？ 钱不够了，那就不能去网吧了。只能直接回家了。<br><img src="https://img-blog.csdnimg.cn/d72b7660b1024197b2c041f3b25e0398.png" alt="在这里插入图片描述"></p>
<h2 id="5-2-if双分支"><a href="#5-2-if双分支" class="headerlink" title="5.2 if双分支"></a>5.2 if双分支</h2><p>那么这时候问题来了，你钱不够，去不了网吧。又不想马上回家，必须要买点东西弥补一下去不了网吧的遗憾。这时就可以用双分支。</p>
<p><img src="https://img-blog.csdnimg.cn/756ac47083d14bd68223ad3dfef6fd3a.png" alt="在这里插入图片描述"><br>因为你不想立刻回家，必须做点事情才回家，那么就没有直接回家这条选项了。</p>
<p>用程序模拟</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> money = <span class="number">4</span>; <span class="comment">//你身上的钱</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span> (money &gt; <span class="number">5</span>) <span class="comment">//如果你身上的钱超过5块</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//去网吧</span></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;去网吧-&gt;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="comment">//钱不够，就跳到这来</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;去买棒棒糖-&gt;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//不管如何，你都要回家</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;回家&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当你只有4块钱时<br><img src="https://img-blog.csdnimg.cn/d7b45a86bde043b7a835328ca1178880.png" alt="在这里插入图片描述"><br>当你有6块钱时<br><img src="https://img-blog.csdnimg.cn/04859dc6fec44757a8c5eb6a7fadcd86.png" alt="在这里插入图片描述"></p>
<h2 id="5-3-if多分支"><a href="#5-3-if多分支" class="headerlink" title="5.3 if多分支"></a>5.3 if多分支</h2><p>那么这时候你又有要求了。 如果身上的钱超过20块，就去游泳，如果不超过20块钱，但是超过10块钱，就去吃肯德基。如果不超过10块钱，但是超过5块钱，就去网吧。五块钱都没有，那就只能吃棒棒糖了。最后再回家。<br><img src="https://img-blog.csdnimg.cn/872f956b6dea458b86cc996267b9eb8e.png" alt="在这里插入图片描述"><br>那么我们就else if</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> money = <span class="number">6</span>; <span class="comment">//你身上的钱</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span> (money &gt; <span class="number">20</span>) <span class="comment">//如果你身上的钱超过20块</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//去游泳</span></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;去游泳-&gt;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span>(money &gt; <span class="number">10</span>)<span class="comment">//钱不够20,但是超过10块</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;去吃肯德基-&gt;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (money &gt; <span class="number">5</span>)<span class="comment">//钱不够10块,但是超过5块</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;去网吧-&gt;&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;去买棒棒糖-&gt;&quot;</span>); <span class="comment">//五块钱都没有</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//不管如何，你都要回家</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;回家&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>21块钱的结果<br><img src="https://img-blog.csdnimg.cn/1f9f6f9bf0e64c3b8fc5d95531d66f27.png" alt="在这里插入图片描述"></p>
<p>11块钱的结果<br><img src="https://img-blog.csdnimg.cn/b7b4e82199b6471e96e2fca5a3383f7b.png" alt="在这里插入图片描述"></p>
<p>6块钱的结果<br><img src="https://img-blog.csdnimg.cn/780f1c8f02ce41ceb86ff6beb34de215.png" alt="在这里插入图片描述"></p>
<p>4块钱的结果<br><img src="https://img-blog.csdnimg.cn/917451d9539c47a7a6d7f9b1b96bc695.png" alt="在这里插入图片描述"></p>
<h2 id="5-4-switch分支"><a href="#5-4-switch分支" class="headerlink" title="5.4 switch分支"></a>5.4 switch分支</h2><blockquote>
<p><font color="#ed83ab" size=4 face="黑体" > switch 分支也是多分支，跟 if else的功能是一样的。只不过在某种特定情况，用switch分支会比 if else分支更加直观，更加方便。</font></p>
</blockquote>
<p>假设 你想制定一个计划表，从周一到周日。每一天都做不同的事情。这种情况用 if else会显得很杂乱，代码可读性并不是很好。</p>
<p>if else的写法</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> weekday = <span class="number">1</span>; <span class="comment">//星期数</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (weekday == <span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;跑步&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (weekday == <span class="number">2</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;爬山&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (weekday == <span class="number">3</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;游泳&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (weekday == <span class="number">4</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;打游戏&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (weekday == <span class="number">5</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;打篮球&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (weekday == <span class="number">6</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;踢足球&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (weekday == <span class="number">7</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;写作业&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这种频繁的 if else ，看的让人眼花缭乱。 所以在这里推荐用switch分支！！</p>
<p>switch分支的写法</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> weekday = <span class="number">1</span>; <span class="comment">//星期数</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (weekday)<span class="comment">//weekday是要匹配的值</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//如果weekday = 1，那么会走这条语句的内容</span></span><br><span class="line">	<span class="keyword">case</span> <span class="number">1</span>: <span class="built_in">printf</span>(<span class="string">&quot;跑步&quot;</span>); </span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//如果weekday = 2，那么会走这条语句的内容</span></span><br><span class="line">	<span class="keyword">case</span> <span class="number">2</span>: <span class="built_in">printf</span>(<span class="string">&quot;爬山&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//如果weekday = 3，那么会走这条语句的内容</span></span><br><span class="line">	<span class="keyword">case</span> <span class="number">3</span>: <span class="built_in">printf</span>(<span class="string">&quot;游泳&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//如果weekday = 4，那么会走这条语句的内容</span></span><br><span class="line">	<span class="keyword">case</span> <span class="number">4</span>: <span class="built_in">printf</span>(<span class="string">&quot;打游戏&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//如果weekday = 5，那么会走这条语句的内容</span></span><br><span class="line">	<span class="keyword">case</span> <span class="number">5</span>: <span class="built_in">printf</span>(<span class="string">&quot;打篮球&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//如果weekday = 6，那么会走这条语句的内容</span></span><br><span class="line">	<span class="keyword">case</span> <span class="number">6</span>: <span class="built_in">printf</span>(<span class="string">&quot;踢足球&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//如果weekday = 7，那么会走这条语句的内容</span></span><br><span class="line">	<span class="keyword">case</span> <span class="number">7</span>: <span class="built_in">printf</span>(<span class="string">&quot;写作业&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//如果上面都不满足，那么会执行这一条</span></span><br><span class="line">	<span class="keyword">default</span>:</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;星期数不存在&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/383f6263ec1a4256a0f0439daba1423c.png" alt="在这里插入图片描述"></p>
<p><font color="#ed83ab" size=4 face="黑体" >注意！每条case分支必须要加上break语句，否则会接着执行下一条语句。假设我把case1，和case2的break删掉。</font><br><img src="https://img-blog.csdnimg.cn/0977e56083074c12bcf9996da5c85041.png" alt="在这里插入图片描述"><br>我们会发现它会接着往下执行，直到遇到break才结束。</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="true"
        data-ae="true"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
        <div class="side">
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E5%AD%97%E7%AC%A6%E4%B8%B2%E3%80%81%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="toc-number">1.</span> <span class="toc-text">4. 字符串、转义字符</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AE%9A%E4%B9%89"><span class="toc-number">1.1.</span> <span class="toc-text">4.2 字符串定义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%95%BF%E5%BA%A6"><span class="toc-number">1.2.</span> <span class="toc-text">4.2 字符串长度</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%A4%A7%E5%B0%8F"><span class="toc-number">1.3.</span> <span class="toc-text">4.3 字符串大小</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4-%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="toc-number">1.4.</span> <span class="toc-text">4.4 转义字符</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E9%80%89%E6%8B%A9%E8%AF%AD%E5%8F%A5"><span class="toc-number">2.</span> <span class="toc-text">5.选择语句</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-if%E5%8D%95%E5%88%86%E6%94%AF"><span class="toc-number">2.1.</span> <span class="toc-text">5.2 if单分支</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-if%E5%8F%8C%E5%88%86%E6%94%AF"><span class="toc-number">2.2.</span> <span class="toc-text">5.2 if双分支</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-3-if%E5%A4%9A%E5%88%86%E6%94%AF"><span class="toc-number">2.3.</span> <span class="toc-text">5.3 if多分支</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-4-switch%E5%88%86%E6%94%AF"><span class="toc-number">2.4.</span> <span class="toc-text">5.4 switch分支</span></a></li></ol></li></ol>
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/my_blog.gitee.io/js/plugin.js"></script>
<script src="/my_blog.gitee.io/js/typed.js"></script>
<script src="/my_blog.gitee.io/js/diaspora.js"></script>


<link rel="stylesheet" href="/my_blog.gitee.io/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/my_blog.gitee.io/photoswipe/default-skin/default-skin.css">


<script src="/my_blog.gitee.io/photoswipe/photoswipe.min.js"></script>
<script src="/my_blog.gitee.io/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
